package com.solveit.aps.io.db.activerecord;

import com.solveit.aps.IdAndNameBasedEntity;
import com.solveit.aps.io.db.InternalDbUtil;
import com.solveit.core.db2.annotations.Column;
import com.solveit.core.db2.annotations.Id;

/**
 * 
 * @author Stanislav
 * 
 * @param <T>
 *            is db model (@Entity)
 */
public abstract class ActiveRecordModel<T> extends IdAndNameBasedEntity{
	public static final int ID = 1;
	@Id(auto = true)
	@Column(name = "id", nullable = false, alias = ID)
	private Integer id = 0;

	public Integer getId() {
		return id;
	}
	public void setId(Integer id){
		this.id = id;
	}

	public void insert() {
		InternalDbUtil.getInstance().insert(this);
	}

	public void update(int... aliases) {
		InternalDbUtil.getInstance().update(this, aliases);
	}

	public void save() {
		if (this.getId() == 0) {
			insert();
		} else {
			update();
		}
	}

	public void delete() {
		InternalDbUtil.getInstance().delete(this);
	}
}